package com.grass.appointment.comment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.base.view.StatusControlLayout;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.CommentAdapter;
import com.grass.appointment.bean.CommentBean;
import com.grass.appointment.bean.CommentContainer;
import com.grass.appointment.bean.ReplyBean;
import com.grass.appointment.comment.CommentVerticalLayout;
import com.grass.appointment.comment.InputTextDialog;
import com.grass.appointment.model.CommentModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import java.util.ArrayList;
import java.util.List;

public class CommentFragment extends BottomSheetDialogFragment implements OnLoadMoreListener, ItemClickListener, CommentVerticalLayout.OnTwoClickListener {
    private CommentAdapter adapter;
    private CommentInterface commentInterface;
    private int commentTotal;
    private int dynamicId;
    private InputTextDialog inputTextDialog;
    private long lastId;
    private CommentModel model;
    private int position;
    private SmartRefreshLayout refresh;
    private StatusControlLayout status_layout;
    private TextView tv_total;
    private int userId;
    private View view;

    public interface CommentInterface {
        void addCommentNum(int i, int i2);
    }

    public static CommentFragment newInstance() {
        Bundle bundle = new Bundle();
        CommentFragment commentFragment = new CommentFragment();
        commentFragment.setArguments(bundle);
        return commentFragment;
    }

    public void setCommentInterface(CommentInterface commentInterface2) {
        this.commentInterface = commentInterface2;
    }

    @Override // androidx.appcompat.app.AppCompatDialogFragment, com.google.android.material.bottomsheet.BottomSheetDialogFragment, androidx.fragment.app.DialogFragment
    public Dialog onCreateDialog(Bundle bundle) {
        BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) super.onCreateDialog(bundle);
        View inflate = View.inflate(getContext(), C2694R.C2699layout.comment_layout, null);
        this.view = inflate;
        bottomSheetDialog.setContentView(inflate);
        ((View) this.view.getParent()).setBackgroundColor(getResources().getColor(17170445));
        bottomSheetDialog.getWindow().findViewById(C2694R.C2697id.design_bottom_sheet).setBackgroundResource(17170445);
        BottomSheetBehavior.from((View) this.view.getParent()).setPeekHeight(UiUtils.dp2px(531));
        return bottomSheetDialog;
    }

    @Override // androidx.fragment.app.Fragment, androidx.fragment.app.DialogFragment
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        initView(this.view);
        this.status_layout.showLoading();
        this.lastId = 0;
        this.model.loadDynamicComment(this.dynamicId, 0);
    }

    private void initView(View view2) {
        this.model = (CommentModel) new ViewModelProvider(this).get(CommentModel.class);
        CommentAdapter commentAdapter = new CommentAdapter();
        this.adapter = commentAdapter;
        commentAdapter.setOnTwoClickListener(this);
        this.adapter.setImgDomain(SpUtils.getInstance().getString("domain"));
        this.adapter.setUserId(this.userId);
        this.status_layout = (StatusControlLayout) view2.findViewById(C2694R.C2697id.status_layout);
        SmartRefreshLayout smartRefreshLayout = (SmartRefreshLayout) view2.findViewById(C2694R.C2697id.refresh);
        this.refresh = smartRefreshLayout;
        smartRefreshLayout.setEnableRefresh(false);
        RecyclerView recyclerView = (RecyclerView) view2.findViewById(C2694R.C2697id.recycler);
        this.status_layout.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.appointment.comment.CommentFragment.View$OnClickListenerC27721 */

            public void onClick(View view) {
                CommentFragment.this.status_layout.showLoading();
                CommentFragment.this.model.loadDynamicComment(CommentFragment.this.dynamicId, CommentFragment.this.lastId);
            }
        });
        this.refresh.setOnLoadMoreListener(this);
        this.adapter.setItemListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(this.adapter);
        TextView textView = (TextView) view2.findViewById(C2694R.C2697id.tv_total);
        this.tv_total = textView;
        textView.setText(this.commentTotal + "條評論");
        this.model.commentList().observe(this, new Observer<BaseRes<CommentContainer>>() {
            /* class com.grass.appointment.comment.CommentFragment.C27732 */

            public void onChanged(BaseRes<CommentContainer> baseRes) {
                CommentFragment.this.hideLoading();
                if (baseRes.getCode() == 200) {
                    List<CommentBean> data = baseRes.getData().getData();
                    if (data == null || data.size() == 0) {
                        CommentFragment.this.status_layout.showEmpty();
                        return;
                    }
                    if (CommentFragment.this.lastId == 0) {
                        CommentFragment.this.adapter.setData(data);
                    } else {
                        CommentFragment.this.adapter.setDatasInEnd(data);
                    }
                    if (data.size() >= 1) {
                        CommentFragment.this.lastId = (long) data.get(data.size() - 1).getCommentId();
                    }
                    if (data.size() != 0 && data.size() < 30) {
                        CommentFragment.this.refresh.finishLoadMore(0, true, true);
                        return;
                    }
                    return;
                }
                CommentFragment.this.status_layout.showError();
            }
        });
        view2.findViewById(C2694R.C2697id.ll_comment).setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.comment.CommentFragment.View$OnClickListenerC27743 */

            public void onClick(View view) {
                if (SpUtils.getInstance().getUserInfo().getFreeWatches() == -1) {
                    CommentFragment.this.initInputTextMsgDialog(null);
                } else {
                    ToastUtils.getInstance().showSigh("VIP用戶才能參與評論哦");
                }
            }
        });
        view2.findViewById(C2694R.C2697id.img_close).setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.comment.CommentFragment.View$OnClickListenerC27754 */

            public void onClick(View view) {
                CommentFragment.this.dismiss();
            }
        });
        this.model.addComment().observe(this, new Observer<BaseRes<CommentBean>>() {
            /* class com.grass.appointment.comment.CommentFragment.C27765 */

            public void onChanged(BaseRes<CommentBean> baseRes) {
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    ToastUtils.getInstance().showWeak("評論失敗");
                    return;
                }
                CommentFragment.this.addCommentBean(baseRes.getData());
                ToastUtils.getInstance().showCorrect("評論成功");
            }
        });
        this.model.addCommentReply().observe(this, new Observer<BaseRes<ReplyBean>>() {
            /* class com.grass.appointment.comment.CommentFragment.C27776 */

            public void onChanged(BaseRes<ReplyBean> baseRes) {
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    ToastUtils.getInstance().showWeak("評論失敗");
                    return;
                }
                CommentFragment.this.addCommentReplyBean(baseRes.getData());
                ToastUtils.getInstance().showCorrect("評論成功");
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void initInputTextMsgDialog(final CommentBean commentBean) {
        dismissInputDialog();
        if (this.inputTextDialog == null) {
            InputTextDialog inputTextDialog2 = new InputTextDialog(getContext(), C2694R.C2701style.dialogCenter);
            this.inputTextDialog = inputTextDialog2;
            inputTextDialog2.setOnTextSendListener(new InputTextDialog.OnTextSendListener() {
                /* class com.grass.appointment.comment.CommentFragment.C27787 */

                @Override // com.grass.appointment.comment.InputTextDialog.OnTextSendListener
                public void onTextSend(String str) {
                    if (commentBean == null) {
                        CommentFragment.this.model.commitComment(CommentFragment.this.dynamicId, str);
                    } else {
                        CommentFragment.this.model.replyComment(commentBean, str);
                    }
                }
            });
        }
        this.inputTextDialog.show();
    }

    private void dismissInputDialog() {
        InputTextDialog inputTextDialog2 = this.inputTextDialog;
        if (inputTextDialog2 != null) {
            if (inputTextDialog2.isShowing()) {
                this.inputTextDialog.dismiss();
            }
            this.inputTextDialog.cancel();
            this.inputTextDialog = null;
        }
    }

    public void hideLoading() {
        this.status_layout.hideLoading();
        this.refresh.finishLoadMore();
    }

    public void loadDynamicComment(int i, int i2, int i3, int i4) {
        this.userId = i;
        this.dynamicId = i2;
        this.commentTotal = i3;
        this.position = i4;
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view2, int i) {
        if (SpUtils.getInstance().getUserInfo().getFreeWatches() != -1) {
            ToastUtils.getInstance().showSigh("VIP用戶才能參與評論哦");
        } else {
            initInputTextMsgDialog((CommentBean) this.adapter.getDataInPosition(i));
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        this.model.loadDynamicComment(this.dynamicId, this.lastId);
    }

    @Override // androidx.fragment.app.DialogFragment
    public void show(FragmentManager fragmentManager, String str) {
        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.add(this, str);
        beginTransaction.commitAllowingStateLoss();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void addCommentBean(CommentBean commentBean) {
        this.status_layout.hideLoading();
        this.adapter.setDataInStart(commentBean);
        this.commentTotal++;
        this.tv_total.setText(this.commentTotal + "條評論");
        CommentInterface commentInterface2 = this.commentInterface;
        if (commentInterface2 != null) {
            commentInterface2.addCommentNum(this.position, this.commentTotal);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void addCommentReplyBean(ReplyBean replyBean) {
        List data = this.adapter.getData();
        for (int i = 0; i < data.size(); i++) {
            CommentBean commentBean = (CommentBean) data.get(i);
            if (replyBean.getParentId() == commentBean.getCommentId()) {
                List<ReplyBean> reply = commentBean.getReply();
                if (reply == null) {
                    reply = new ArrayList<>();
                }
                reply.add(0, replyBean);
                commentBean.setReply(reply);
                this.adapter.setItemRefresh(true);
                this.adapter.notifyItemChanged(i, Integer.valueOf(C2694R.C2697id.layout_comment2));
            }
        }
        this.commentTotal++;
        this.tv_total.setText(this.commentTotal + "條評論");
        CommentInterface commentInterface2 = this.commentInterface;
        if (commentInterface2 != null) {
            commentInterface2.addCommentNum(this.position, this.commentTotal);
        }
    }

    @Override // com.grass.appointment.comment.CommentVerticalLayout.OnTwoClickListener
    public void onTwoClick(ReplyBean replyBean, int i) {
        if (SpUtils.getInstance().getUserInfo().getFreeWatches() != -1) {
            ToastUtils.getInstance().showSigh("VIP用戶才能參與評論哦");
        } else {
            initInputTextMsgTwoDialog(replyBean, i);
        }
    }

    private void initInputTextMsgTwoDialog(final ReplyBean replyBean, final int i) {
        dismissInputDialog();
        if (this.inputTextDialog == null) {
            InputTextDialog inputTextDialog2 = new InputTextDialog(getContext(), C2694R.C2701style.dialogCenter);
            this.inputTextDialog = inputTextDialog2;
            inputTextDialog2.setOnTextSendListener(new InputTextDialog.OnTextSendListener() {
                /* class com.grass.appointment.comment.CommentFragment.C27798 */

                @Override // com.grass.appointment.comment.InputTextDialog.OnTextSendListener
                public void onTextSend(String str) {
                    CommentFragment.this.model.replyTwoComment(replyBean, str, i);
                }
            });
        }
        this.inputTextDialog.show();
    }

    @Override // com.google.android.material.bottomsheet.BottomSheetDialogFragment, androidx.fragment.app.DialogFragment
    public void dismiss() {
        dismissAllowingStateLoss();
    }
}
